package com.lemon.smartsafetynew.ui.activity.monitor;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.monitor.MonitorDetailItemVo;
import com.lemon.smartsafetynew.entity.monitor.MonitorItemVo;
import com.lemon.smartsafetynew.net.services.MonitorServices;
import com.lemon.smartsafetynew.ui.adapter.monitor.MonitorDetailItemAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/8/8.
 */

public class MonitorDetailActivity extends SwipeBackActivity
        implements RecyclerTClickListener<MonitorDetailItemVo> {

    @Bind(R.id.monitor_title_tv)
    TextView monitorTitleTv;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private static MonitorItemVo itemVo;

    private MonitorDetailItemAdapter monitorDetailItemAdapter;
    private List<MonitorDetailItemVo> mDataList = new ArrayList<>();

    private Timer timer;

    @Override
    protected void initTitle() {
        this.setBarTitle("监测详情");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_monitor_detail;
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle;
        bundle = getIntent().getExtras();

        if (bundle != null) {
            itemVo = (MonitorItemVo) bundle.getSerializable(KeyConstants.KEY_MONITOR_ITEM_VO);
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        //initHeadView();
        initRecyclerView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        startGetNetData();
    }

    @Override
    public void onItemClick(MonitorDetailItemVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_MONITOR_SENSOR_INFO, data);
        bundle.putSerializable(KeyConstants.KEY_MONITOR_ITEM_VO, itemVo);
        UIHelper.showActivity(this, bundle, MonitorSensorActivity.class);
    }

    private void initHeadView(List<MonitorDetailItemVo> list) {

        if (list == null || list.size() == 0) {
            return;
        }

        monitorTitleTv.setText(itemVo.getName());
    }


    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        monitorDetailItemAdapter = new MonitorDetailItemAdapter(mDataList, this);
        recyclerView.setAdapter(monitorDetailItemAdapter);
    }

    private void getNetData() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("topic", itemVo.getPlaceId());
        MonitorServices.init().getMonitorPointSensorList(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_MONITOR_SENSOR_LIST.equals(event.getTag())) {  //获得监测点传感器列表
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow() && recyclerView != null) {
                mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (monitorDetailItemAdapter != null) {
                        mDataList.clear();
                        monitorDetailItemAdapter.notifyDataSetChanged();
                        monitorDetailItemAdapter.addItems((List<MonitorDetailItemVo>) event.getObj());
                    }

                    initHeadView((List<MonitorDetailItemVo>) event.getObj());

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }
        }
    }

    /**
     * 按频率获取一次数据
     */
    private final static int TIMES = 5; //5s一次

    private void startGetNetData() {

        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                getNetData();
            }
        }, 0, 1000 * TIMES);
    }

    /**
     * 清理对象，缓存等
     */
    private void clear() {

        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_MONITOR_SENSOR_LIST);
    }

    @Override
    protected void onPause() {
        super.onPause();
        clear();
    }
}
